<?php 
namespace Common\Model;

use Think\Model;
class BaseModel extends Model{
    private static $agentList = null;
    protected $autoCheckFields = true;
    
    function __construct($name='',$tablePrefix='',$connection=''){
        if(empty($name) || get_class($this) == 'Common\Model\BaseModel'){
            $this->autoCheckFields = false;
        }
        parent::__construct($name, $tablePrefix, $connection);
    }
    
    /**
     * 下单时代理价
     * @param unknown $level
     * @param unknown $product
     * @return unknown
     */
    public function getAgentPrice($level, $product){
        $agent = $this->agentLevel($level);
        return $product[$agent['price_field']];
    }
    
    /*代理级别*/
    public function agentLevel($level = ''){
        if(is_null(self::$agentList)){
            $list = $this->query("SELECT id, `level`, title, price_title, price_field FROM agent ORDER BY `level`");
            self::$agentList = array();
            foreach($list as $k=>$v){
                self::$agentList[$v['level']] = $v;
            }
        }
    
        if($level !== ''){
            return self::$agentList[$level];
        }
        return self::$agentList;
    }
    
    public function goodsHandler($goods, $myLevel){
        $goods["agent_level"] = $myLevel;
        $agentList = $this->agentLevel();
        
        // 代理价格
        $goods['agents'] = array();
        end($agentList);
        $lastLevel = key($agentList);
        foreach($agentList as $level=>$agent){
            $shenmi = $myLevel == 0 && $level==$lastLevel;
            $goods['agents'][$level] = array(
                'level'         => $level,
                'title'         => $agent['title'],
                'price'         => $shenmi ? '神秘' : $goods[$agent['price_field']],
                'price_title'   => $agent['price_title'],
                'visible'       => ($myLevel == 0 && $level == $lastLevel) || ($myLevel > 0 && $level >= $myLevel),
                'price_prefix'  =>  $shenmi ? '' : '¥',
                'price_suffix'  => ''
            );
        }

        if(is_string($goods["images"])){
            $goods["images"] = json_decode($goods["images"], true);
        }
        if(!empty($goods['sku_json']) && is_string($goods['sku_json'])){
            $goods['sku_json'] = json_decode($goods["sku_json"], true);
        }

        // 打标签
        $goods['tags'] = '';
        if($goods['freight_type'] == 0){
            $goods['tags'] = '包邮';
        }

        unset($goods['agent0_price']);
        unset($goods['agent1_price']);
        unset($goods['agent2_price']);
        unset($goods['agent3_price']);
        unset($goods['agent4_price']);
        return $goods;
    }
    
    /**
     * 获取微信用户信息
     */
    public function getWXUserConfig($mid, $openid = ''){
        $sql = "SELECT wx_user.mid, wx_user.openid, wx_user.appid, wx_user.subscribe, wx_user.nickname, member.nickname AS `name`, member.pid, member.id
                FROM member
                INNER JOIN wx_user ON member.id=wx_user.mid
                WHERE member.id='{$mid}' 
                ORDER BY subscribe DESC, last_login DESC";
        $list = $this->query($sql);
        if(count($list) == 0){
            return;
        }
        $wxUser = $list[0];
        
        // 找当前openid
        if(!empty($openid) && count($list) > 1){
            foreach($list AS $item){
                if($item['openid'] == $openid){
                    if($item['subscribe'] == 1){
                        $wxUser = $item;
                    }
                    break;
                }
            }
        }

        if($wxUser['subscribe'] != 1){
            return $wxUser;
        }
        
        $wxUser['config'] = get_wx_config($wxUser['appid']);
        return $wxUser;
    }
    
    /**
     * 获取店铺信息
     */
    public function getAllShop(){
        return $this->query("SELECT id, `name` FROM shop");
    }
}
?>